package com.fare.taluo.core.domain.crawl;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;

import com.dayatang.domain.Entity;
import com.dayatang.domain.InstanceFactory;
import com.dayatang.domain.QuerySettings;
import com.fare.taluo.base.ddd.domain.baseentity.BaseEntity2;
import com.fare.taluo.crawl.CrawlConst;

/**
 * 抓取任务执行日志记录
 * <p>在<@link>TaskQueueEntity</link>队列执行过程中，每一个队列每执行一次产生一条日志记录
 * @author xiangsf 2013-2-17
 *
 */
@javax.persistence.Table(name="CRAWL_TASK_QUEUE_LOG")
@javax.persistence.Entity
public class TaskQueueLogEntity extends BaseEntity2<TaskQueueLogEntity> implements Entity{

	private static final long serialVersionUID = 7924245433935886065L;
//	@javax.persistence.Id
//	@javax.persistence.Column(name="id", length=32)
//	@javax.persistence.GeneratedValue(generator="system-uuid")
//	@org.hibernate.annotations.GenericGenerator(name="system-uuid", strategy="uuid")
//	private String id;

	@Column(name = "task_id")
	private String taskId;
	
	@Column(name = "record_name")
	private String recordName;
	
	@Column(name = "remarks")
	private String remarks;
	
	@Column(name = "create_date")
	private Date createTime = new Date();
	
	@Column(name = "start_time")
	private Date startTime;
	
	@Column(name = "end_time")
	private Date endTime;
	
	@Column(name = "status")
	private Integer status = CrawlConst.CRAWL_MONITOR_FINISH;
	
	@Column(name = "crawl_count")
	private int crawlCount = 0;
	
	@Column(name = "from_city")
	private String fromCity;
	
	@Column(name = "from_city_name")
	private String fromCityName;
	
	@Column(name = "to_city")
	private String toCity;
	
	@Column(name = "to_city_name")
	private String toCityName;
	
	@Column(name = "flight_date")
	private String flightDate;
	
	@Column(name = "RETURN_GRAB_DATE")
	private String returnGrabDate;
	
	@Column(name = "GRAB_CHANNEL_ID")
	private Long grabchannelID;
	
	@Column(name = "grab_channel")
	private String grabChannel;
	
	@Column(name = "proxy_ip")
	private String proxyIp;
	
	@Column(name = "thread_name")
	private String threadName;
	
	@Column(name ="ATTACH_HBASE_KEY")
	private String HbaseKey;
	
	
	/**
	 *流量大小
	 */
	@Column(name = "byte_length")
	private long byteLength;
	
	public long getByteLength() {
		return byteLength;
	}


	public void setByteLength(long byteLength) {
		this.byteLength = byteLength;
	}

	/**
	 * 获取代理ip花费时间
	 */
	@Column(name = "proxy_ip_duration_time")
	private Long proxyIpDurationTime = 0L;
	
	/**
	 * 抓取源网页内容花费时间
	 */
	@Column(name = "crawl_duration_time")
	private Long crawlDurationTime = 0L;
	
	/**
	 * 解析源网页内容花费时间
	 */
	@Column(name = "parse_duration_time")
	private Long parseDurationTime = 0L;
	
	/**
	 * 存储解析后内容花费时间
	 */
	@Column(name = "store_duration_time")
	private Long storeDurationTime = 0L;
	
	/**
	 * 存储在hbase中的源网页数据rowkey值
	 */
	@Column(name = "source_rowkey")
	private String sourceRowkey;
	
	/**
	 * 抓取的网页大小
	 */
	@Column(name = "SOURCE_DATA_SIZE")
	private Long sourceDataSize;
	
	public TaskQueueLogEntity(){}
	

//	public String getId() {
//		return id;
//	}
//
//	public void setId(String id) {
//		this.id = id;
//	}

	public String getFromCity() {
		return fromCity;
	}

	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}

	public String getFlightDate() {
		return flightDate;
	}

	public void setFlightDate(String flightDate) {
		this.flightDate = flightDate;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public String getRecordName() {
		return recordName;
	}

	public void setRecordName(String recordName) {
		this.recordName = recordName;
	}

	public int getCrawlCount() {
		return crawlCount;
	}

	public void setCrawlCount(int crawlCount) {
		this.crawlCount = crawlCount;
	}


	public String getFromCityName() {
		return fromCityName;
	}

	public void setFromCityName(String fromCityName) {
		this.fromCityName = fromCityName;
	}



	public String getToCity() {
		return toCity;
	}

	public void setToCity(String toCity) {
		this.toCity = toCity;
	}

	public String getToCityName() {
		return toCityName;
	}

	public void setToCityName(String toCityName) {
		this.toCityName = toCityName;
	}


	public String getGrabChannel() {
		return grabChannel;
	}

	public void setGrabChannel(String grabChannel) {
		this.grabChannel = grabChannel;
	}

	public String getThreadName() {
		return threadName;
	}

	public void setThreadName(String threadName) {
		this.threadName = threadName;
	}

	public String getProxyIp() {
		return proxyIp;
	}

	public void setProxyIp(String proxyIp) {
		this.proxyIp = proxyIp;
	}

	public Long getProxyIpDurationTime() {
		return proxyIpDurationTime;
	}

	public void setProxyIpDurationTime(Long proxyIpDurationTime) {
		this.proxyIpDurationTime = proxyIpDurationTime;
	}

	public Long getCrawlDurationTime() {
		return crawlDurationTime;
	}

	public void setCrawlDurationTime(Long crawlDurationTime) {
		this.crawlDurationTime = crawlDurationTime;
	}

	public Long getParseDurationTime() {
		return parseDurationTime;
	}

	public void setParseDurationTime(Long parseDurationTime) {
		this.parseDurationTime = parseDurationTime;
	}

	public Long getStoreDurationTime() {
		return storeDurationTime;
	}

	public void setStoreDurationTime(Long storeDurationTime) {
		this.storeDurationTime = storeDurationTime;
	}
	
	
	
	public String getReturnGrabDate() {
		return returnGrabDate;
	}


	public void setReturnGrabDate(String returnGrabDate) {
		this.returnGrabDate = returnGrabDate;
	}


	public String getSourceRowkey() {
		return sourceRowkey;
	}

	public void setSourceRowkey(String sourceRowkey) {
		this.sourceRowkey = sourceRowkey;
	}

	public Long getGrabchannelID() {
		return grabchannelID;
	}


	public void setGrabchannelID(Long grabchannelID) {
		this.grabchannelID = grabchannelID;
	}

	public String getHbaseKey() {
		return HbaseKey;
	}


	public void setHbaseKey(String hbaseKey) {
		HbaseKey = hbaseKey;
	}


	//	private static TaskQueueLogEntityRepository repository = InstanceFactory.getInstance(TaskQueueLogEntityRepository.class);
	private static TaskQueueLogEntityRepository getTaskQueueLogEntityRepository(){
		return InstanceFactory.getInstance(TaskQueueLogEntityRepository.class);
	}
	
	public void update() {
		getRepository().save(this);

	}

	public void remove() {
		getRepository().remove(this);
	}
	
	public List<TaskQueueLogEntity> findAll() {
		return getRepository().findAll(TaskQueueLogEntity.class);
	}
	
	
	//持久化处理
	public List<TaskQueueLogEntity> getAllDatasByGrabDate(Date grabDate,Date endGrabDate){
		return getTaskQueueLogEntityRepository().getAllDatasByGrabDate(grabDate,endGrabDate);
	}
	
	public List<TaskQueueLogEntity> getTaskRecordList(String fromCityOrg,String toCityOrg,
			String grabChannel,Date startTime,Date endTime,String status,String sortorder,String sortname,int page,int pagesize){
		
		return getTaskQueueLogEntityRepository().getTaskRecordList(fromCityOrg, toCityOrg, grabChannel, startTime, 
				endTime, status ,sortorder,sortname, page ,pagesize);
	}
	
	public int getTotalTaskRecordListByCodition(String fromCityOrg,String toCityOrg,
			String grabChannel,Date startTime,Date endTime,String status){
		
		return getTaskQueueLogEntityRepository().getTotalTaskRecordListByCodition(fromCityOrg, toCityOrg, grabChannel, startTime, 
				endTime, status);
	}
	
	public List<TaskQueueLogEntity> getTaskRecordByGDate(Date beginTime,Date endTime,String grabchannel,String status){
		return getTaskQueueLogEntityRepository().getTaskRecordByGDate(beginTime,endTime, grabchannel,status);
	}
	
	public List<TaskQueueLogEntity> getTaskRecordByNowDate(Date nowDate){
		return getTaskQueueLogEntityRepository().getTaskRecordByNowDate(nowDate);
	}

	//引擎统计 抓取失败详细信息查询
	public List<TaskQueueLogEntity> getTaskRecordByDateAndChannel(Date grabdate,String grabchannelID,String status,int page,int pagesize){
		return getTaskQueueLogEntityRepository().getTaskRecordByDateAndChannel(grabdate,grabchannelID,status,page,pagesize);
	}
	
	//引擎统计 Total抓取失败详细信息查询
		public List<TaskQueueLogEntity> getTotalRecordByDateAndChannel(Date grabdate,String grabchannelID,String status,String fromCity,String toCity,int page,int pagesize){
			return getTaskQueueLogEntityRepository().getTotalRecordByDateAndChannel(grabdate,grabchannelID,status,fromCity,toCity,page,pagesize);
		}
	//fail and tatal
		public int getTotalAndFailCountByDateAndChannel(Date grabdate,String grabchannelID,String fromCity, String toCity){
			return getTaskQueueLogEntityRepository().getTotalAndFailCountByDateAndChannel(grabdate, grabchannelID,fromCity ,toCity);
		}
		public List<TaskQueueLogEntity> getTotalAndFailRecordByDateAndChannel(Date grabdate,String grabchannelID,String fromCity, String toCity,int page,int pagesize){
			return getTaskQueueLogEntityRepository().getTotalAndFailRecordByDateAndChannel(grabdate,grabchannelID,fromCity,toCity,page,pagesize);
		}
	//引擎统计 total详细信息查询  
		public int getTotalCountByDateAndChannel(Date grabdate,String grabchannelID,String status ,String fromCity, String toCity){
			return getTaskQueueLogEntityRepository().getTotalCountByDateAndChannel(grabdate, grabchannelID,status,fromCity ,toCity);
		}

	public int getCountByDateAndChannel(Date grabdate,String grabchannelID,String status){
		return getTaskQueueLogEntityRepository().getCountByDateAndChannel(grabdate, grabchannelID,status);
	}

	public Long getSourceDataSize() {
		return sourceDataSize;
	}


	public void setSourceDataSize(Long sourceDataSize) {
		this.sourceDataSize = sourceDataSize;
	}


	@Override
	public boolean isNew() {
		return getId() == null ;
	}

	@Override
	public boolean existed() {
		if (isNew()) {
			return false;
		}
		return getRepository().exists(getClass(), id);
	}

	@Override
	public boolean notExisted() {
		return ! existed();
	}
	
	@Override
	public boolean existed(String propertyName, Object propertyValue) {
		List<?> entities = getRepository().find(QuerySettings.create(getClass()).eq(propertyName, propertyValue)); 
		return !(entities.isEmpty());
	}
}
